package net.spmiller;

import java.io.File;
import java.util.*;

public class GeotagRescuer {

    private FlickrInterface flickr;
    private FileSystemInterface fileSystem;

    public GeotagRescuer(FlickrInterface flickr, FileSystemInterface fileSystem) {
        this.flickr = flickr;
        this.fileSystem = fileSystem;
    }

    public List<FlickrPhoto> getUngeotaggedPhotos() {
        return flickr.getUngeotaggedPhotos("wahooie_stuie");
    }

    public List<FileSystemPhoto> getGeotaggedPhotosFrom(File directory) {
        return fileSystem.getGeotaggedPhotosFrom(directory);
    }


    public List<FlickrPhoto> updateGeotags(List<FlickrPhoto> unGeotaggedPhotos, List<? extends Photo> geotaggedPhotos) {
        final Map<String, Photo> geotaggedPhotosIndex = new HashMap<String, Photo>(geotaggedPhotos.size());

        for (Photo photo : geotaggedPhotos) {
            geotaggedPhotosIndex.put(photo.getUniqueId(), photo);
        }

        List<FlickrPhoto> newlyGeotaggedPhotos = new ArrayList<FlickrPhoto>();
        for (FlickrPhoto photo : unGeotaggedPhotos) {
            Photo geotaggedPhoto = geotaggedPhotosIndex.get(photo.getUniqueId());
            Location location = null;
            if (geotaggedPhoto != null) {
                location = new Location(geotaggedPhoto.getLocation());
            }
            newlyGeotaggedPhotos.add(new FlickrPhoto(photo, location));
        }

        return newlyGeotaggedPhotos;
    }
}
